//
//  XLHomePresenter.m
//  industry
//
//  Created by xl on 2020/11/12.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLHomePresenter.h"
#import "XLHomePresenterProtocol.h"
#import "XLHomeInteractorProtocol.h"
#import "XLAppletModel.h"
#import "XLHomeIndexModel.h"
#import "XLHomeFuncModel.h"
#import "XLHomeViewProtocol.h"
#import "XLAppletManager.h"
#import "XLPopupModel.h"

@interface XLHomePresenter ()<XLHomePresenterProtocol>

@property (nonatomic, strong) NSArray *commonArray;
@property (nonatomic, strong) NSArray *allArray;

@end
@implementation XLHomePresenter

- (void)dealloc {
    XLLog(@"XLHomePresenter dealloc");
}

#pragma mark - XLHomePresenterProtocol
- (void)handleCellData:(id)cellData {
    if ([cellData isKindOfClass:[XLAppletModel class]]) {
        XLAppletModel *model = (XLAppletModel *)cellData;
        if (model.isMore) {//更多
            XLSend(self.baseController.interactor, XLHomeInteractorProtocol, gotoAllApplyVC:self.commonArray allArr:self.allArray);
        } else {
            XLAppletManager *manager = [[XLAppletManager alloc] init];
            [manager handleJumpLogic:@(1) url:model.appletUrl appId:model.appId];
        }
    }
}

- (void)requestHomeAllInfo {
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    WEAKSELF
    [request requestWithMethod:XLHttpMedthTypeGET url:XLHomePageListUrl params:nil isExtraParams:NO successBlock:^(NSDictionary *dict) {
        STRONGSELF
        id data = [dict objectForKey:@"data"];
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSDictionary *temDic = (NSDictionary *)data;
            id allData = [temDic objectForKey:@"appletEntities"];
            id indexData = [temDic objectForKey:@"dataIndicatorsEntities"];
            id funcData = [temDic objectForKey:@"funcBarEntities"];
            id commonData = [temDic objectForKey:@"oftenUseAppletEntities"];
            id popupData = [temDic objectForKey:@"popups"];
            
            NSArray *allArr;
            NSArray *indexArr;
            NSArray *funcArr;
            NSArray *commonArr;
            NSArray *popupArr;
            if ([allData isKindOfClass:[NSArray class]]) {
                allArr = [XLAppletModel mj_objectArrayWithKeyValuesArray:allData];
                self.allArray = allArr;
            }
            if ([indexData isKindOfClass:[NSArray class]]) {
                indexArr = [XLHomeIndexModel mj_objectArrayWithKeyValuesArray:indexData];
            }
            if ([funcData isKindOfClass:[NSArray class]]) {
                funcArr = [XLHomeFuncModel mj_objectArrayWithKeyValuesArray:funcData];
            }
            if ([commonData isKindOfClass:[NSArray class]]) {
                commonArr = [XLAppletModel mj_objectArrayWithKeyValuesArray:commonData];
                self.commonArray = commonArr;
                commonArr = (commonArr.count > 7) ? [commonArr subarrayWithRange:NSMakeRange(0, 7)]:commonArr;
                
                NSMutableArray *temArr = [NSMutableArray arrayWithArray:commonArr];
                XLAppletModel *model = [[XLAppletModel alloc] init];
                model.appletName = @"更多";
                model.isMore = YES;
                [temArr addObject:model];
                commonArr = temArr;
                
            }
            if ([popupData isKindOfClass:[NSArray class]]) {
                popupArr = [XLPopupModel mj_objectArrayWithKeyValuesArray:popupData];
            }
            XLSend(strongSelf.baseController.cView, XLHomeViewProtocol, refreshCollectionView:allArr indexArr:indexArr funcArr:funcArr commonArr:commonArr popupArr:popupArr);
        }

    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
        
    }];
}

- (void)refreshCommonArr:(NSArray *)arr {
    self.commonArray = arr;
}


- (void)requestIndexsOrder:(NSArray *)arr {
    NSMutableArray *temArr = [NSMutableArray array];
    for (XLHomeIndexModel *model in arr) {
        if (!ISEmptyNumber(model.indicatorsId)) {
            [temArr addObject:model.indicatorsId.stringValue];
        }
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (temArr.count > 0) {
        NSString *indexStr = [temArr componentsJoinedByString:@","];
        [params setValue:indexStr forKey:@"indicatorsId"];
    }
    
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    [request requestWithMethod:XLHttpMedthTypePOST url:XLReFreshIndexOrderUrl params:params isExtraParams:NO successBlock:^(NSDictionary *dict) {
        
    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
        
    }];
}

@end
